#Boa:Dialog:ComRulesABM

import wx
import wx.grid

import datetime

import ruleManager
import ComRulesEdition

def create(parent):
    return ComRulesABM(parent)

[wxID_COMRULESABM, wxID_COMRULESABMBORRAR, wxID_COMRULESABMBUTTON1, 
 wxID_COMRULESABMEDITAR, wxID_COMRULESABMLISTCTRL1, 
 wxID_COMRULESABMMONTHCOMBO, wxID_COMRULESABMMONTHLBL, wxID_COMRULESABMNUEVO, 
 wxID_COMRULESABMSTATICTEXT1, wxID_COMRULESABMSTATICTEXT2, 
 wxID_COMRULESABMSTATICTEXT3, wxID_COMRULESABMYEARCOMBO, 
] = [wx.NewId() for _init_ctrls in range(12)]

class ComRulesABM(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_COMRULESABM, name='', parent=prnt,
              pos=wx.Point(0, 0), size=wx.Size(598, 468),
              style=wx.DEFAULT_DIALOG_STYLE, title='Reglas')
        self.SetClientSize(wx.Size(582, 430))

        self.Nuevo = wx.Button(id=wxID_COMRULESABMNUEVO, label='Nuevo',
              name='Nuevo', parent=self, pos=wx.Point(496, 128),
              size=wx.Size(75, 23), style=0)
        self.Nuevo.Bind(wx.EVT_BUTTON, self.OnNewButton,
              id=wxID_COMRULESABMNUEVO)

        self.Editar = wx.Button(id=wxID_COMRULESABMEDITAR, label='Editar',
              name='Editar', parent=self, pos=wx.Point(496, 160),
              size=wx.Size(75, 23), style=0)
        self.Editar.Bind(wx.EVT_BUTTON, self.OnUpdateButton,
              id=wxID_COMRULESABMEDITAR)

        self.Borrar = wx.Button(id=wxID_COMRULESABMBORRAR, label='Borrar',
              name='Borrar', parent=self, pos=wx.Point(496, 192),
              size=wx.Size(75, 23), style=0)
        self.Borrar.Bind(wx.EVT_BUTTON, self.OnDeleteButton,
              id=wxID_COMRULESABMBORRAR)

        self.yearCombo = wx.ComboBox(choices=[], id=wxID_COMRULESABMYEARCOMBO,
              name='yearCombo', parent=self, pos=wx.Point(112, 16),
              size=wx.Size(130, 21), style=wx.CB_READONLY, value='comboBox1')

        self.monthCombo = wx.ComboBox(choices=[], id=wxID_COMRULESABMMONTHCOMBO,
              name='monthCombo', parent=self, pos=wx.Point(112, 48),
              size=wx.Size(130, 21), style=wx.CB_READONLY, value='comboBox2')

        self.staticText1 = wx.StaticText(id=wxID_COMRULESABMSTATICTEXT1,
              label='A\xf1o:', name='staticText1', parent=self, pos=wx.Point(24,
              16), size=wx.Size(24, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_COMRULESABMSTATICTEXT2,
              label='Mes', name='staticText2', parent=self, pos=wx.Point(24,
              48), size=wx.Size(20, 13), style=0)

        self.button1 = wx.Button(id=wxID_COMRULESABMBUTTON1, label='Cargar',
              name='button1', parent=self, pos=wx.Point(280, 48),
              size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnLoadButton,
              id=wxID_COMRULESABMBUTTON1)

        self.staticText3 = wx.StaticText(id=wxID_COMRULESABMSTATICTEXT3,
              label='Reglas de ', name='staticText3', parent=self,
              pos=wx.Point(24, 104), size=wx.Size(51, 13), style=0)

        self.monthLbl = wx.StaticText(id=wxID_COMRULESABMMONTHLBL, label='',
              name='monthLbl', parent=self, pos=wx.Point(80, 104),
              size=wx.Size(0, 13), style=0)

        self.listCtrl1 = wx.ListCtrl(id=wxID_COMRULESABMLISTCTRL1,
              name='listCtrl1', parent=self, pos=wx.Point(16, 128),
              size=wx.Size(472, 288), style=wx.SUNKEN_BORDER | wx.LC_REPORT)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.year = datetime.date.today().year
        self.month = datetime.date.today().month
        
        self.refreshMonthLabel()
        self.populateYearAndMonths()
        self.createGrid()
        self.populateGrid()

    def populateYearAndMonths(self):
        for year in range(2010, 2030):
            self.yearCombo.Append(str(year))
            
        self.yearCombo.SetValue(str(self.year))
            
        for month in range(1, 13):
            self.monthCombo.Append(str(month))
            
        self.monthCombo.SetValue(str(self.month))

    def createGrid(self):             
        columnas =['Regla', 'Puntos']        
        for i, col in enumerate(columnas):
            self.listCtrl1.InsertColumn(i, heading=col)
            self.listCtrl1.SetColumnWidth(i, 130)
            
        self.listCtrl1.SetColumnWidth(0, 300)      
        
    def populateGrid(self):
        self.listCtrl1.DeleteAllItems()    
        self.rules = ruleManager.get_rules(self.year, self.month)
        for i, r in enumerate(self.rules):  
            index = self.listCtrl1.InsertStringItem(i, r.name)
            self.listCtrl1.SetStringItem(index, 1, str(r.points), 0)           
            
    def refreshMonthLabel(self):
        self.monthLbl.SetLabel("%s - %s" % (self.year, self.month))

    def OnNewButton(self, event):
        v = ComRulesEdition.ComRulesEdition(self, self.year, self.month, None)
        v.ShowModal()
        self.populateGrid()       

    def OnUpdateButton(self, event):
        rule = self.GetSelectedRule()
        v = ComRulesEdition.ComRulesEdition(self, self.year, self.month, rule)
        v.ShowModal()          
        self.populateGrid()

    def OnDeleteButton(self, event):
        dlg = wx.MessageDialog(self, "Desea borrar la regla?", style=wx.YES_NO)
        if dlg.ShowModal() == wx.ID_YES:        
            r = self.GetSelectedRule()
            ruleManager.delete_rule(r)
            self.populateGrid()
        
        dlg.Destroy()

    def GetSelectedRule(self):
        selected_row = self.listCtrl1.GetFirstSelected()
        print selected_row
        for x in self.rules:
            print x.name
            
        r = self.rules[selected_row]
        return r

    def OnLoadButton(self, event):
        self.year = self.yearCombo.GetValue()
        self.month = self.monthCombo.GetValue()
        self.refreshMonthLabel()
        self.populateGrid()     
